package com.lhh.videoservice.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Video;
import com.lhh.springcommon.entity.vo.VideoVO;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.springcommon.mapper.VideoMapper;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.videoservice.feign.AlgorithmFeign;
import com.lhh.videoservice.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李厚航
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SearchServiceImpl implements SearchService {

    private final AlgorithmFeign algorithmFeign;

    private final VideoMapper videoMapper;

    @Autowired
    public SearchServiceImpl(AlgorithmFeign algorithmFeign, VideoMapper videoMapper) {
        this.algorithmFeign = algorithmFeign;
        this.videoMapper = videoMapper;
    }

    @Override
    @WebLog
    public Page<VideoVO> recommendVideoByUid(PageQuery<String> page) {
        Integer userId = JwtInterceptor.getUserIdFromThreadLocal();
        Integer num = page.getPage()* page.getPageSize();
        List<Integer> algorithm = algorithmFeign.algorithm(userId, num);
        //通过分页信息提取视频信息
        return videoMapper.queryVideoVOByIds(algorithm,new Page<>(page.getPage(), page.getPageSize()));
    }

    @Override
    @WebLog
    public Long queryVideoCountByKeyword(Video video) {
        return videoMapper.searchCount(video);
    }

}
